<?php
/**
 * 《招聘职位》实现类
 *
 * @author 孙晓晔
 * @version $Id: Visit.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Job extends InfoUtil {
	public $tbl = Constant::tbl_zp;
	
	public $type = array(
		'-' => '请选择',
		'1' => '全职',
		'2' => '兼职',
	);
	
	public $sex = array(
		'-' => '请选择',
		'1' => '男',
		'2' => '女',
		'3' => '不限',
	);
	
	public $education = array(
		'-' => '请选择',
		'1' => '小学',
		'2' => '初中',
		'3' => '高中',
		'4' => '中专',
		'5' => '大专',
		'6' => '本科',
		'7' => '硕士',
	);
	
	public $address = array(
		'-' => '请选择',
		'深圳宝安沙井同富裕工业区' => '深圳宝安沙井同富裕工业区',
		'深圳南山南园枫叶大厦' => '深圳南山南园枫叶大厦',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '草稿',
		'2' => '发布',
		'3' => '暂停',
		'4' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '职位',
		'orderby' => '排序',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'oper' => '录入员',
		'status' => '状态',
	
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('type_option', $this->type);
		$controller->assign_by_ref('sex_option', $this->sex);
		$controller->assign_by_ref('education_option', $this->education);
		$controller->assign_by_ref('address_option', $this->address);
	}
	
	function tab($controller, $request) {		
		$data = array(
			'招聘职位' => array('cc'=>'info.php?do=Job.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'info/job/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$sex_s = $request['sex_s'];
		$education_s = $request['education_s'];

		$where = " WHERE 1=1";

		if (!empty($sex_s) && $sex_s <> '-') {
			$where .= " AND sex = '" . $sex_s . "'";
		}
		
		if (!empty($education_s) && $education_s <> '-') {
			$where .= " AND education = '" . $education_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/job/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('1 month'));
		
		$controller->display($request, 'info/job/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$name = addslashes($request['name']);		
		$type = $request['type'];
		
		$num = $request['num'];
		$sex = $request['sex'];
		$age = $request['age'];
		$education = $request['education'];
		
		$pay = trim($request['pay']);
		$address = trim($request['address']);
		
		$work = addslashes($request['work']);
		$content = addslashes($request['content']);
		$expired = $request['expired'];

		$oper = $_SESSION['id'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, type, num, sex, age, education, pay, work, content, expired, emp, oper, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $name, $type, $num, $sex, $age, $education, $pay, $work, $content, $expired, $oper, $oper);
		debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$orderby = range(0, 12, 1);
		$controller->assign_by_ref('orderby_option', $orderby);

		$controller->display($request, 'info/job/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$name = addslashes($request['name']);		
		$type = $request['type'];
		
		$num = $request['num'];
		$sex = $request['sex'];
		$age = $request['age'];
		$education = $request['education'];
		
		$pay = trim($request['pay']);
		$address = trim($request['address']);
		
		$work = addslashes($request['work']);
		$content = addslashes($request['content']);
		$expired = $request['expired'];
		$orderby = $request['orderby'];
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET name = '%s', type = '%s', num = '%s', sex = '%s', age = '%s', education = '%s', pay = '%s', address = '%s', ";
		$fmt .= "work = '%s', content = '%s', expired = '%s', orderby = '%s', emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $type, $num, $sex, $age, $education, $pay, $address, $work, $content, $expired, $orderby, $emp, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/job/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['type_t'] = $this->type[$row['type']];
		$row['sex_t'] = $this->sex[$row['sex']];
		$row['education_t'] = $this->education[$row['education']];
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function emp($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);
		
		$controller->display($request, 'info/job/emp');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function empPost($controller, $request) {
		$this->pretreat($controller);

		$job = addslashes($request['job']);
				
		$name = $request['name'];
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		
		$password = $request['password'];
		
		$idcard = $request['idcard'];
		$sex = $request['sex'];
		$birthday = $request['birthday'];
		
		$province = $request['province'];
		$city = $request['city'];		
		$district = trim($request['district']);
		
		$address = trim($request['address']);
		
		$content = addslashes($request['content']);

		$oper = $_SESSION['id'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, zjm, password, office, job, idcard, sex, birthday, province, city, district, address, content, oper, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '0', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, Constant::tbl_emp, $name, $zjm, $password, $job, $idcard, $sex, $birthday, $province, $city, $district, $address, $content, $oper);
		debug($sql);
		$db->query($sql);
		$id = $db->insert_id();
		
		$fmt = "UPDATE %s SET code = id WHERE id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp, $id);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
function delete($controller, $request) {
		$this->pretreat($controller);
		
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function ajax_oper($controller, $request) {
		$id = $_COOKIE['id_'];
		$ret = array();
		
		if ($this->_ajax_x($id, 'oper')) {
			$ret['status'] = 1;
		} else if ($this->_ajax_x($id, 'emp')) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}

		echo json_encode($ret);
	}
}
?>
